<?php

class SearchController extends Controller
{
	/**
	 * Declares class-based actions.
	 */
	public function actions()
	{
		return array(
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		$isSearch = false;
		if(isset($_GET['q']))
		{
			$q = $_GET['q'];
			try{
				$sql = 'select * from tbl_sii where match(title, content) against("'.$q.'" IN BOOLEAN MODE)';
				$rows = Yii::app()->db->cache(10000)->createCommand($sql)->queryAll();
				$isSearch = true;
			}
			catch(Exception $e) {
     	   		echo $e->getMessage();
    		}
	    }

		$this->render('index', array('q'=>$q, 'rows'=>$rows, 'isSearch'=>$isSearch));
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
		if($error=Yii::app()->errorHandler->error)
		{
			if(Yii::app()->request->isAjaxRequest)
				echo $error['message'];
			else
				$this->render('error', $error);
		}
	}
}
